<template>
  <div class="user-main">
    <el-form :inline="true" :model="page" size="mini" class="demo-form-inline pagination">
      <el-form-item label="姓名">
        <el-input v-model="page.params.name" clearable placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="岗位">
        <el-select v-model="page.params.jobsId" clearable placeholder="请选择岗位">
          <el-option
            v-for="item in typeList"
            :key="item.id"
            :label="item.jobsName"
            :value="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="mini" @click="onSubmit">查询</el-button>
      </el-form-item>
    </el-form>
    <el-divider style="margin: 0;" />
    <div style="margin:15px 0px 0px 15px;">用户列表</div>
    <el-button type="primary" style="margin:15px 0px 0px 15px;" @click="openDisplay()">添加</el-button>
    <el-button type="primary" style="margin:15px 0px 0px 15px;" @click="openExcel()">
      excel导入 <svg-icon icon-class="importExcel" />
    </el-button>
    <!--列表-->
    <el-table :data="page.list" border style="width: 100%;margin:15px;" @sort-change="changeSort">
      <el-table-column prop="name" label="姓名" align="center" width="150" show-overflow-tooltip />
      <el-table-column prop="phone" label="账号" align="center" width="150"/>
      <el-table-column prop="jobsId" label="岗位" align="center" width="150"> 
        <template slot-scope="scope">
          <div v-for="item in typeList" :key="item.id">
            <el-tag v-if="scope.row.jobsId === item.id">{{item.jobsName}}</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="headerUrl" align="center" label="头像" width="150">
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.headerUrl"
            :preview-src-list="[scope.row.headerUrl]" />
        </template>
      </el-table-column>
      <el-table-column prop="createTime" align="center" label="创建时间" width="180" sortable="custom" />
      <el-table-column prop="status" width="150" align="center" label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0" type="success">启用</el-tag>
          <el-tag v-else type="info">禁止登陆</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-dropdown>
            <el-button size="mini" type="primary">
              操作<i class="el-icon-arrow-down el-icon--right" />
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item><el-button size="mini" type="primary" @click="handleEdit(scope.row)">修改</el-button></el-dropdown-item>
              <el-dropdown-item><el-button v-if="scope.row.status === 1" size="mini" type="success" @click="toEnable(scope.row.id)">启用</el-button></el-dropdown-item>
              <el-dropdown-item><el-button v-if="scope.row.status === 0" size="mini" type="warning" @click="toDisable(scope.row.id)">弃用</el-button></el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="pagination"
      :current-page="page.currentPage"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="page.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.totalCount"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"/>
    <!--添加对话框-->
    <el-dialog
      width="50%"
      title="添加用户"
      :visible.sync="addDialog">
      <user-add @closeDisplay="closeDisplay" @getPageList="getPageList" />
    </el-dialog>
        <!--添加对话框-->
    <el-dialog
      width="50%"
      title="用户信息导入"
      :visible.sync="exportDialog">
        <el-button type="primary" style="margin:15px 0px 15px 0px;">
          <a :href="file.url"><svg-icon icon-class="upload" /> 下载{{file.name}}</a>
        </el-button>
        <el-upload
          class="upload-demo"
          drag
          :action="uploadUrl"
          :headers="header"
          :on-success="handleSuccess"
          multiple>
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
          <div class="el-upload__tip" slot="tip">请下载模板后,修改内容进行上传文件</div>
        </el-upload>
    </el-dialog>
    <!--修改对话框-->
    <el-dialog
      width="50%"
      title="用户信息"
      :visible.sync="update">
      <user-update v-if="update" :user="user" @closeDisplay="closeDisplay" @getPageList="getPageList" />
    </el-dialog>
  </div>
</template>

<script>

import userApi from '@/api/users'
import fileApi from '@/api/file'
import userAdd from './user-add'
import { getToken } from '@/utils/auth'
import userUpdate from './user-update'
export default {
  components: {
    userAdd,
    userUpdate
  },
  data() {
    return {
      header: {
        Authorization: getToken()
      },
      uploadUrl: process.env.VUE_APP_USER_URL, //  图片上传路径
      typeList: null,
      update: false,
      addDialog: false,
      exportDialog:false,
      read: false,
      page: {
        currentPage: 1,
        pageSize: 10,
        totalPage: 0,
        totalCount: 0,
        params: {},
        list: [],
        sortColumn: 'createTime',
        sortMethod: 'desc'
      },
      user:{},
      file:{}
    }
  },
  created() {
    this.getPageList()
    setTimeout(() => {
      this.typeList = this.$store.getters.typeList
    })

  },
  methods: {
    handleSuccess(res, file) {
      this.$message.success("失败条数:"+res.data.fail+","+"成功条数:"+res.data.success)
      this.getPageList()
    },
    openExcel(){
      fileApi.queryExcel(1).then(res => {
        this.file = res.data
      })
      this.exportDialog = true
    },
    handleSizeChange(val) {
      this.page.pageSize = val
      this.getPageList()
    },
    handleCurrentChange(val) {
      this.page.currentPage = val
      this.getPageList()
    },
    getPageList() {
      this.page.list = []
      userApi.pageList(this.page).then(res => {
        this.page = res.data
      })
    },
    openDisplay() {
      this.addDialog = true
    },
    closeDisplay() {
      this.addDialog = false
      this.update = false
    },
    toEnable(id){
      userApi.enable(id).then(res => {
        this.$message.success(res.data)
        this.getPageList()
      })
    },
    toDisable(id){
      userApi.delete(id).then(res => {
        this.$message.success(res.data)
        this.getPageList()
      })
    },
    handleEdit(row) {
      this.user = row
      this.update = true
    },
    onSubmit() {
      this.getPageList()
    },
    changeSort(e) {
      if (e.order) {
        this.page.sortColumn = e.prop
        if(e.order=="ascending")
          this.page.sortMethod = "asc"
        else
          this.page.sortMethod = "desc"
      } else {
        this.page.sortColumn = "id"
        this.page.sortMethod = 'asc'
      }
      this.getPageList()
    }
  }
}
</script>

<style scoped>
.user-main{
  padding: 2%;
  background-color: #f5f5f5;
}
</style>
